/**
 * 
 */
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Hashtable;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import Action.News;
import Action.Topic;
import Bean.NewsBean;
import Bean.TopicBean;

import Login.CheckLogin;
import Utils.Constant;
import Utils.DbAccess;
import Utils.FileUtils;
import Utils.GenUtils;
import Utils.ImageUtils;
import Utils.Random;
import Utils.StringUtil;
import Utils.Thumbnail;
import Utils.ZipResponse;
import Utils.upload.FileUpload;
import Utils.upload.Section;






public class InsertUpdateTopic extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 4194452284080389577L;
	
	DbAccess db = null;
	
	public void init() {
		try {
			db = new DbAccess(Constant.DB,true);
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.setContentType("text/html");
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");
		if(!CheckLogin.check(req)){
			ZipResponse.write(
				req,
				res,
				"<html><script>window.location='blank.htm';window.parent.onAddTopic('notlogin','');</script></html>",
				"UTF-8");
			return;
		}
		Connection conn = null;
		String folder1="";
		try{
			conn = db.getConn();
			String rootPath = req.getRealPath("/");
			if (!rootPath.endsWith("/"))
				rootPath += "/";
			folder1 =rootPath + "WEB-INF/uploads/news/"+Random.randomString(3);
			File f = new File(folder1);
			if (!f.exists())
				f.mkdirs();
			FileUpload fileUpload = new FileUpload(f.getAbsolutePath(), 0);
			Hashtable uploads = null;
			uploads = fileUpload.getUpload(req, res);
			String action = ((Section) uploads.get("action")).getValue().trim();
			String title = ((Section)uploads.get("title")).getValue().trim();
			String createdDate = StringUtil.convertDateForSQL(((Section)uploads.get("createdDate")).getValue().trim());
			String catId = ((Section)uploads.get("catId")).getValue().trim();
			String topicId = "";
			if(action.equals("insert")){
				topicId = Random.randomString(5);	
			}else{
				topicId =  ((Section)uploads.get("topicId")).getValue().trim();
				
			}
			
			String folder = GenUtils.genPathByAccountId(topicId);
			String img = ImageUtils.uploadImage(uploads, folder);
			TopicBean bean = new TopicBean();
			bean.setTopicId(topicId);
			bean.setTopicName(title);
			if(img.equals("")) 
				img = ((Section)uploads.get("thumbnail")).getValue().trim();
			bean.setThumbnail(img);
			bean.setDatePublished(createdDate);
			bean.setCatId(catId);
			if(action.equals("insert")){
				if(new Topic().insertTopic(bean, conn)){
					ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onAddTopic('true','');</script></html>",
								"UTF-8");
					return;
				}
			}else{
				if(new Topic().updateTopic(bean, conn)){
					ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onAddTopic('true','');</script></html>",
								"UTF-8");
					return;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null){try{conn.close();}catch (Exception e) {}}
			FileUtils.deleteDir(new File(folder1));
		}
		ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onAddTopic('false','');</script></html>",
				"UTF-8");
	}
	
}
